package com.parkingspace.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.parkingspace.entity.Property;
import com.parkingspace.service.impl.PropertyServiceImpl;
import com.parkingspace.util.Constants;
import com.parkingspace.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 物业表 前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@RestController
@RequestMapping("/property")
public class PropertyController {

    @Autowired
    private PropertyServiceImpl propertyService;

//    @GetMapping ("/getPropertyByName")
//    public ResultVO  getPropertyByName(@PathVariable Property property) {
////    public List<Property>  getPropertyByName(Property property) {
//        System.out.println("进来了+++++++"+property.getPropertyName());
//        ResultVO resultVO;
//        List<Property> propertyList = propertyService.getPropertyByName(property.getPropertyName());
//
//        if (propertyList.size() == 0){
//            resultVO = new ResultVO(Constants.OPER_FAILURE,"查询失败",propertyList);
//            return resultVO;
//        }
//        resultVO = new ResultVO(Constants.OPER_SUCCESS,"成功",propertyList);
//
//        return resultVO;
////        return  propertyList;
//    }

    @GetMapping ("/getPropertyList")
    public ResultVO  getPropertyList(String token,Property property, Page page) {
        System.out.println("进来了+++++++（全查）"+property.getPropertyName());
        ResultVO resultVO;
        List<Property> propertyList;
        IPage pages=null;
        if (token==null){
            return null;
        }
        if (property.getPropertyName()==""||property.getPropertyName()==null) {
            System.out.println("1");
            pages = propertyService.getPropertyList(token,property.getPropertyName(),page);
            System.out.println("controller查到的："+pages);
        }else {
            System.out.println("2");
            pages = propertyService.getPropertyList(token,property.getPropertyName(),page);
            System.out.println("pages(Controller) = " + pages);
        }

        if (pages==null){
            return new ResultVO(Constants.OPER_FAILURE,"查询失败1",pages);
        }
        return new ResultVO(Constants.OPER_SUCCESS,"成功",pages);
    }

    @PostMapping("/addProperty")
    public ResultVO  addProperty(Property property) {
        System.out.println("增加物业进来了"+property.getPropertyDetails());
        System.out.println("增加物业进来了"+property.getPropertyName());
        System.out.println("增加物业进来了"+property.getPropertyShare());
        System.out.println("增加物业进来了"+property.getPropertySoftdel());
        ResultVO resultVO;
        boolean flag;
        if (property!=null) {
            flag =propertyService.addProperty(property);
        }

        if (flag=false){
            return new ResultVO(Constants.OPER_FAILURE,"增加失败",false);
        }
        return new ResultVO(Constants.OPER_SUCCESS,"增加成功",true);
    }

    @PutMapping("/delProperty")
    public ResultVO  delPropertyById(Long propertyId) {
        System.out.println("删除物业进来了"+propertyId);
//        ResultVO resultVO;
        boolean flag;
        if (propertyId!=null&&propertyId>0) {
            flag =propertyService.delPropertyById(propertyId);
        }

        if (flag=true){
            return new ResultVO(Constants.OPER_SUCCESS,"删除成功",true);

        }
        return new ResultVO(Constants.OPER_FAILURE,"删除失败",false);
//    return new ResultVO(Constants.OPER_FAILURE,"删除失败",false);
    }

    @GetMapping("/getProperty")
    public ResultVO  getPropertyById(Long propertyId) {
        System.out.println("根据id物业进来了"+propertyId);
//        ResultVO resultVO;
        Property property=null;
        if (propertyId!=null&&propertyId>0) {
            property = propertyService.getPropertyById(propertyId);
            System.out.println("查到了");
        }

        if (property!=null){
            return new ResultVO(Constants.OPER_SUCCESS,"根据id查询成功",property);

        }
        return new ResultVO(Constants.OPER_FAILURE,"根据id查询失败",null);
//    return new ResultVO(Constants.OPER_FAILURE,"删除失败",false);
    }

    @PutMapping("/updProperty")
    public ResultVO  updPropertyById(Property property) {
        System.out.println("根据id修改物业信息进来了1："+property.getPropertyId());
        System.out.println("根据id修改物业信息进来了2："+property.getPropertyName());
        System.out.println("根据id修改物业信息进来了3："+property.getPropertyShare());
        System.out.println("根据id修改物业信息进来了4："+property.getPropertyDetails());
        System.out.println("根据id修改物业信息进来了5："+property.getPropertySoftdel());

//        ResultVO resultVO;
        if (property!=null) {
            boolean flag= propertyService.updPropertyById(property);
        }

        if (property!=null){
            return new ResultVO(Constants.OPER_SUCCESS,"根据id查询成功",property);

        }
        return new ResultVO(Constants.OPER_FAILURE,"根据id查询失败",null);
//    return new ResultVO(Constants.OPER_FAILURE,"删除失败",false);
    }



}



